package com.jbm.jbmsupplier.zp.FactouryActivity;

import android.content.Context;
import android.content.Intent;

import com.jbm.jbmsupplier.zp.entity.Buddy_ListData;
import com.jbm.jbmsupplier.zp.entity.ChatMsgEntity;
import com.jbm.jbmsupplier.zp.utils.ClassUser;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by 家帮忙 on 2017/7/11.
 */

public class MyNewText implements ChatSaveMessage {
    private ArrayList<HashMap<String, String>> list1;
    private ArrayList<String> odername;
    private  Context context;
    private ArrayList<ChatMsgEntity> arrayList;
    private ClassUser ClassUser;
    private final int USERMASSAGES=0x004;
    private final int TIME=0x0001;
    @Override
    public void setDataChat(Context context, ArrayList<ChatMsgEntity> arrayList,ClassUser ClassUser) {
        this.context=context;
        Buddy_ListData namse;
//        if (arrayList.get(0).getTags()==1){
//            MySqliteUtlis.GetInstan(context).InsertGroadTable(CHATGROUD, MySqliteUtlis.GetInstan(context).SelectName(GROUD, arrayList.get(0).getCgid()) + "", OHInterface.getG_server().logo, arrayList.get(0).getDate(), arrayList.get(0).getText(), "", arrayList.get(0).getTo_id(), -1, -1,-0,"", "", "", "", 1, "", "", 8, arrayList.get(0).getCgid(), 1,"true",ClassUser.getLogin());
//        }else{
//        list1 = MySqliteUtlis.GetInstan(context).showalldata(BUDDY ,arrayList.get(0).getTo_id());
//        if (list1.toString()==null) {
//                 isOn = false;
//                hadnler.sendEmptyMessageDelayed(TIME, 2000);
//        } else if (!list1.toString().contains(arrayList.get(0).getTo_id()+"")){
//            isOn = false;
//            hadnler.sendEmptyMessageDelayed(TIME, 2000);
//        }else {
            /**
             * 查找好友的名字是否存在
             * 这个可能性出现  退出登录再次登录
             * 名字就会丢失
             * 因为刚进来直接执行接收消息
             * 还没来的及执行获取名字
             * 不存在就添加好友
             */
//                                   namse = MySqliteUtlis.GetInstan(context).showFriendsToID(BUDDY, arrayList.get(0).getTo_id(), ClassUser.getLogin());
//                                     if (namse==null) {
//                                         MySqliteUtlis.GetInstan(context).StoringBuddydata(BUDDY , arrayList.get(0).getTo_id(),"默认名(家)","","","","","","","",1,ClassUser.getLogin());
//                                         MySqliteUtlis.GetInstan(context).StoringChatData(CHAT, arrayList.get(0).getTo_id(), OHInterface.getG_server().logo, arrayList.get(0).getText(), arrayList.get(0).getDate(), "", "", "", "", "", "", "", "", "", "", 8, 0, "true", ClassUser.getLogin());
//                                           }else{
//                                         MySqliteUtlis.GetInstan(context).StoringChatData(CHAT, arrayList.get(0).getTo_id(), OHInterface.getG_server().logo,  arrayList.get(0).getText(), arrayList.get(0).getDate(), namse.getNickname(), "", "", "", "", "", "", "", "", "", 8, 0, "true", ClassUser.getLogin());
//                                       }
//                ArrayList<String> als = MySqliteUtlis.GetInstan(context).showaRecentContactsid(RECENT,arrayList.get(0).getTo_id(),ClassUser.getLogin());
//                if (!als.toString().contains(arrayList.get(0).getTo_id()+"")) {
//                    MySqliteUtlis.GetInstan(context).StoringRecentID(RECENT, arrayList.get(0).getTo_id(),ClassUser.getLogin(), arrayList.get(0).getDate());
//                }else if (als.toString()==null){
//                    MySqliteUtlis.GetInstan(context).StoringRecentID(RECENT ,arrayList.get(0).getTo_id(),ClassUser.getLogin(), arrayList.get(0).getDate());
//                }


//        }
//    }
        setBroadCase(8,arrayList);
        arrayList.remove(0);
    }



    /**
     * 发送广播
     * 更新消息页面的数据
     * 和
     * 好友聊天页面的数据
     * @param id
     * @param arrayList
     */
    private void setBroadCase(int id,ArrayList<ChatMsgEntity> arrayList){
        Intent intent = new Intent();
        //messageFlag 布局标识  8代表文本
        intent.putExtra("messageFlag", id);
        if (arrayList.get(0).getTags()==1){
            intent.putExtra("cgid", arrayList.get(0).getCgid());
            intent.putExtra("tags",1);
        }else {
            intent.putExtra("account_id", arrayList.get(0).getTo_id());
        }
        intent.putExtra("text", arrayList.get(0).getText());
        intent.putExtra("Date", arrayList.get(0).getDate());
        intent.setAction("MSG");
        context.sendBroadcast(intent);
    }
}
